package com.ewanghuiju.app.ui.watermail.activity;

import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.watermail.WaterShopDetailsContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.PintuanGoodsSpecsResponBean;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.presenter.pintuan.WaterShopDetailsPresenter;
import com.ewanghuiju.app.test.bean.PintuanHomeBannerBean;
import com.ewanghuiju.app.ui.pintuan.adapter.PintuanShopDetailsAdapter;
import com.ewanghuiju.app.ui.pintuan.adapter.pictureselector.GlideEngine;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.FixedAspectRatioFrameLayout;
import com.ewanghuiju.app.widget.popupwindow.PintuanFuwuPopup;
import com.ewanghuiju.app.widget.popupwindow.PintuanYoufeiPopup;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.muddzdev.styleabletoast.StyleableToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 水滴商品详情
 * @time 2020/2/18
 */
public class WaterShopDetailsActivity extends BaseActivity<WaterShopDetailsPresenter> implements WaterShopDetailsContract.View {

    @BindView(R.id.roll_view_pager)
    XBanner rollViewPager;
    @BindView(R.id.nsv_view)
    NestedScrollView nsvView;
    @BindView(R.id.now_price)
    TextView nowPrice;
    @BindView(R.id.tv_sale)
    TextView tvSale;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_to_top)
    ImageView ivToTop;
    @BindView(R.id.fix_frameLayout)
    FixedAspectRatioFrameLayout fixFrameLayout;
    @BindView(R.id.recyleview_details)
    RecyclerView recyleviewDetails;

    private Animation showAnimation, dismissAnimation;

    private List<PintuanGoodsSpecsResponBean> pintuanGoodsSpecsResponBeanList;

    //当前商品id
    private int shopId = -1;
    private WaterShopResponBean waterShopResponBean;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_water_shop_details;
    }

    @Override
    protected void initEventAndData() {
        shopId = getIntent().getIntExtra(Constants.WATER_SHOP_ID, -1);
        if (shopId == -1) {
            StyleableToast.makeText(mContext, "获取商品失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
            onBackPressedSupport();
            return;
        }
        nsvView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Rect scrollRect = new Rect();
                nsvView.getHitRect(scrollRect);
                //子控件在可视范围内（至少有一个像素在可视范围内）
//                if (scrollY <= cardViewPingjia.getY() && ivToTop.getVisibility() == View.VISIBLE) {//向下滚动
//                    ivToTop.setVisibility(View.GONE);
//                } else if (scrollY > cardViewPingjia.getY() && ivToTop.getVisibility() == View.GONE) {
//                    ivToTop.setVisibility(View.VISIBLE);
//                }
            }
        });

        //弹框执行动画
        float fromX = 0;
        float fromY = 1f;
        float toX = 0;
        float toY = 0;
        showAnimation = createTranslateAnimation(fromX, toX, fromY, toY);
        dismissAnimation = createTranslateAnimation(toX, fromX, toY, fromY);
        LoadingDialogUtils.show(mContext);
        mPresenter.getShopInfo(shopId + "");
    }

    PintuanFuwuPopup pintuanFuwuPopup;
    PintuanYoufeiPopup pintuanYoufeiPopup;

    @OnClick({R.id.iv_back, R.id.tv_exchange, R.id.layout_fuwu_more, R.id.layout_youfei_more,
            R.id.iv_to_top, R.id.iv_kefu})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressedSupport();
                break;
            case R.id.tv_exchange:
                //我要兑换
                if (waterShopResponBean == null) {
                    StyleableToast.makeText(mContext, "获取商品信息失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                new StartActivityUtil(mContext, WaterSureOrderActivity.class)
                        .putExtra(Constants.WATER_SURE_ORDER_INFO, waterShopResponBean)
                        .startActivity(true);
                break;
            case R.id.layout_fuwu_more:
                if (pintuanFuwuPopup == null) {
                    pintuanFuwuPopup = new PintuanFuwuPopup(mContext);
                }
                pintuanFuwuPopup.setText(getString(R.string.fuwu_dh_text));
                pintuanFuwuPopup.setPopupGravity(Gravity.BOTTOM);
                pintuanFuwuPopup.showPopupWindow();
                break;
            case R.id.layout_youfei_more:
                if (pintuanYoufeiPopup == null) {
                    pintuanYoufeiPopup = new PintuanYoufeiPopup(mContext);
                }
                pintuanYoufeiPopup.setPopupGravity(Gravity.BOTTOM);
                pintuanYoufeiPopup.showPopupWindow();
                break;
            case R.id.iv_to_top:
                nsvView.scrollTo(0, 0);
                ivToTop.setVisibility(View.GONE);
                break;
            case R.id.iv_kefu:
                //客服
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_KEFU)
                        .putExtra(Constants.WEBURL_TITLE, Constants.WEBURL_KEFU_TITLE).startActivity(true);
                break;
            default:
                break;
        }
    }

    /**
     * 获取商品详情成功
     *
     * @param waterShopResponBean
     */
    @Override
    public void showShopInfo(WaterShopResponBean waterShopResponBean) {
        LoadingDialogUtils.dismissDialog_ios();
        if (waterShopResponBean == null) {
            StyleableToast.makeText(mContext, "获取商品信息失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        this.waterShopResponBean = waterShopResponBean;
        nowPrice.setText(waterShopResponBean.getPrice());
        tvSale.setText("已兑换" + waterShopResponBean.getSales() + " 件");
        tvTitle.setText(waterShopResponBean.getGoods_title());
        initBanner();
        initShopDetails(waterShopResponBean.getInfo());
    }

    /**
     * 加载商品详情部分  多张图片
     */
    public void initShopDetails(List<String> strList) {
        if (strList == null) {
            strList = new ArrayList<>();
        }
        recyleviewDetails.setHasFixedSize(true);
        recyleviewDetails.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        PintuanShopDetailsAdapter pintuanShopDetailsAdapter = new PintuanShopDetailsAdapter(R.layout.item_pintuan_shop_details);
        recyleviewDetails.setLayoutManager(linearLayoutManager);
        recyleviewDetails.setAdapter(pintuanShopDetailsAdapter);
        pintuanShopDetailsAdapter.setNewData(strList);
    }

    @Override
    public void refreshShopInfo() {
        if (shopId == -1) {
            return;
        }
        mPresenter.getShopInfo(shopId + "");
    }

    /**
     * 加载banner
     */
    public void initBanner() {
        List<String> mImageList = StringUtil.getStringListBySplit(waterShopResponBean.getMulti_image());
        if (mImageList.size() > 0) {
            List<LocalMedia> selectList = new ArrayList<>();
            List<PintuanHomeBannerBean> bannerList = new ArrayList<>();
            for (String urlStr : mImageList) {
                selectList.add(new LocalMedia(urlStr, 1000, PictureMimeType.ofImage(), null));
                bannerList.add(new PintuanHomeBannerBean(urlStr));
            }
            rollViewPager.setBannerData(bannerList);
            //加载广告图片
            rollViewPager.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {
                    ImageView mImageView = (ImageView) view;
                    mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    ImageLoader.load(mContext, bannerList.get(position).getImg(), mImageView);
                }
            });
            rollViewPager.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    PictureSelector.create(mContext)
                            .themeStyle(R.style.picture_default_style) // xml设置主题
                            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)// 设置相册Activity方向，不设置默认使用系统
                            .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                            .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                            .openExternalPreview(position, selectList);
                }
            });
        }
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).statusBarView(R.id.top_view)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
